#!/usr/bin/python3.5

# -*- coding: utf-8 -*-


def bubbleSort(arr):
    '''

    :param arr:
    :return:
    '''
    for i in range(len(arr)):
        for j in range(len(arr)-1):
            if arr[j] > arr[j+1]:
                arr[j] , arr[j+1]= arr[j+1] , arr[j]
    return arr


def bubbleSort_2(arr):
    '''
    :param arr:
    :return:
    设置标志位可以提前结束
    '''
    for i in range(len(arr)):
        flag = True
        for j in range(len(arr) - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
                flag = False
        if flag:
            break
    return arr




if __name__ =="__main__":
    a = [2, 8, 4, 5, 9, 4, 42, 0, 3, 6, 8]
    print(bubbleSort_2(a))